/*
 * Copyright 2008, 2009 Kobi Krasnoff
 * 
 * This file is part of "Catch Falling Coins".
 *
 * "Catch Falling Coins" is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * "Catch Falling Coins" is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with "Catch Falling Coins".  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class SplashWithText extends Canvas implements CommandListener, Runnable {
	private MainMIDlet parent;	
	private Image screenshot;
	private String message;
	private Font font;
	private Command mainMenu = new Command("Back", Command.BACK, 1);
	private Thread t;
	
	public SplashWithText(MainMIDlet parent, int timeout, String message)
	{
		this.parent = parent;
		this.message = message;
		
		try {
            // Set up this canvas to listen to command events
            setCommandListener(this);
            // Add the Exit command
            addCommand(new Command("Go to Menu", Command.EXIT, 1));
        } catch(Exception e) {
            e.printStackTrace();
        }
		
		try {
            screenshot = Image.createImage("/images/area03_bkg0.PNG");
            System.out.println("success");
        }
        catch(IOException e) {
        	System.out.println("Error: " + e.getMessage());
        }
        
        font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
        
        t = new Thread(this);
		t.start();
	}
	
	protected void paint(Graphics g) {
		g.drawImage(screenshot, 0, 0, Graphics.TOP | Graphics.LEFT);
		
		g.setFont(font);
		g.setColor(255, 255, 255);
		g.drawString(message, (this.getWidth() - font.stringWidth(message)) / 2, (this.getHeight() - font.getHeight()) / 2, Graphics.TOP | Graphics.LEFT);
	}
	
	public void commandAction(Command command, Displayable displayable) {
		mySetCurrent();
	}
	
	public void run()
	{
		try 
		{
			Thread.sleep(3000);
		}
		catch (InterruptedException e) {}
		mySetCurrent();
	}
	
	private void mySetCurrent()
	{
		if (message == "Welcome Screen") {
            parent.setCurrent("CopyrightForm");
        }
		else
		{
			parent.setCurrent("MainList");
		}
	}
}
